/*
* @Author: dengjiayao
* @Date:   2017-08-28 17:03:17
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-09-01 16:38:10
*/

import Vue from 'vue'

const SUCCESS = 1000000
const API_ROOT = server.root + '/manage/content'

const announcementListResource = Vue.resource(API_ROOT + '/getnoticelist')
export const getAnnouncementList = ({dispatch}, params) => {
  return announcementListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('ANNOUNCEMENT_LIST_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const addAnnouncementResource = Vue.resource(API_ROOT + '/createnotice')
export const addAnnouncement = ({dispatch}, params) => {
  return addAnnouncementResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('ANNOUNCEMENT_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const updateAnnouncementResource = Vue.resource(API_ROOT + '/updatenotice')
export const updateAnnouncement = ({dispatch}, params) => {
  return updateAnnouncementResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('ANNOUNCEMENT_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const deleteAnnouncementResource = Vue.resource(API_ROOT + '/deletenotice')
export const deleteAnnouncement = ({dispatch}, params) => {
  return deleteAnnouncementResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('ANNOUNCEMENT_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
